package com.itheima.health.controller;

import com.itheima.health.common.BaseContext;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConstant;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：贝
 * @date ：Created in 2022年06月08日
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private MemberDao memberDao;

    //体检成功展示 需要两个表 order member
    @GetMapping("/findById")
    public Result findById(Integer id){
        //日志
        log.info("id={}",id);

        //根据id查询到数据
        Order order = orderService.findById(id);

        //根据id查到预约时间和预约类型
        Date orderDate = order.getOrderDate();
        String orderType = order.getOrderType();

        //获取套餐表id,根据id获取套餐名数据
        Integer setmealId = order.getSetmealId();
        Setmeal setmeal = setMealDao.selectById(setmealId);
        String setmealname = setmeal.getName();

        //获取会员表id,根据id获取会员名字
        Integer memberId = order.getMemberId();
        Member member = memberDao.selectById(memberId);
        String name = member.getName();

        //把数据存储到Map集合里,返回给前端
        Map<String, Object> map = new HashMap();
         map.put("orderType",orderType);
         map.put("member",name);
         map.put("orderDate",new SimpleDateFormat("yyyy-MM-dd").format(orderDate));//需要对时间进行一个转换,格式化下
         map.put("setmeal",setmealname);


        return new Result(true, MessageConst.ORDER_SUCCESS,map);
    }

    //体检预约
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberService memberService;

    @PostMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam param){
        log.info("[param]:{}",param);
        //第一步  验证验证码
        //找键
        String redisKey = RedisConstant.VALIDATE_CODE_PREFIX +"_ORDER_"+param.getTelephone();
        //这种key的写法是遵循一种开发默认规则 不同公司规则不同
        //从redis 获取  验证码
        String code = redisTemplate.opsForValue().get(redisKey).toString();
        log.info("[code]:{}",code);

            //2-创建会员 memberService
            //将param里面 也就是用户填入的信息 赋给member对象 然后交给memberService里的updateByTelephone方法
//        进行判断  没有这个电话号码的信息 就加入新会员   如果有  将信息 更新上（登录时自动注册的新用户是没有姓名、身份证号的信息的）
//        如果用户真的需要体检，需要用自己的身份信息进行预约，这个时候再将身份信息补全
            Member member = new Member();
            member.setIdCard(param.getIdCard());
            member.setPhoneNumber(param.getTelephone());
            member.setName(param.getName());
            member.setSex(param.getSex());
            member = memberService.updateBytelephone(member);

            //3-提交体检信息  orderService
            //
            Order order = new Order();
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setSetmealId(param.getSetMealId());
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setOrderDate(param.getOrderDate());
            order.setMemberId(member.getId());
            order = orderService.add(order);

            return new Result(true, MessageConst.ORDER_SUCCESS,order);



    }
}
